<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<style>
    td {
        width: 50px;
        height: 50px;
        background: yellow;
        border-radius: 50%;
        cursor: pointer;
    }
    img {
        width: 500px;
    }

    .select {
        background: red;
    }
</style>
<body>
    <table>
        <tr>
            <td></td>
            <td></td>
            <td></td>
        </tr>
    </table>
    <img src="" alt="">
</body>
<script>
    var tds = document.querySelectorAll('td');//得到所有的td
    var img = document.querySelector('img')//得到img
    for (var i = 0; i < tds.length; i++) {//for循环
    //因为我的图片的名称为1.jpg 2.jpg  3.jpg
    //正好与td的索引加1，所对应
        tds[i].index = i//保存下i
        tds[i].onclick = function () {//点击事件
            img.src = (this.index + 1) + '.jpg'//给img一个新的src
            var s = document.querySelector('.select')//得到页面上
            //class的值为select的td
            if (s !== null) {
                s.className = ''//如果不为空 清空
            }
            this.className = 'select'//给正在点击的td一个class
        }
    }
</script>

</html>
